<?php
namespace app\index\controller;


use think\cache\driver\Redis;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Index extends Controller
{
//        1:百度
//        2:360
//        3:百度移动端
//        4:sogou
//        5:神马
//      1010://百度
//		1015://360
//		7010://百度移动
//		1030://搜狗
//		7070://神马
//		7015://360移动
//      7030://搜狗移动
//		7080: //头条



    public function set_redis(){
        $redis = new \Redis();
        $redis->connect('127.0.0.1',6379);
        $redis->delete("name"); //先清空
        $rows=Db::table('tb_webkey')->where(['state'=>1,'nowPm'=>['between',[1,20]]])->order('id desc')->limit(3)->select();
        foreach ($rows as $k =>$v){
            $redis->lpush('name',json_encode($v));
//            $redis->sAddArray("name",json_encode($v));
//            $redis->rPush("name",json_encode($v));
//            $redis->lPushx("name",json_encode($v));
        }

        $arList = $redis->lrange("name", 0 ,-1);
//        dump(json_decode($arList[0]));
        dump(($arList));
//

//        $redis->rPushx("name", $rows);

//        // 获取存储的数据并输出
//        $arList = $redis->lrange("name", 0 ,-1);
////        $arList = $redis->smembers('name');
//        dump($arList) ;
    }

    public function update_redis(){
        $redis = new \Redis();
        $redis->connect('127.0.0.1',6379);
        $arList = $redis->lPop('wbKeyList'); //输出并删除
        if ($arList){
            echo ("<script>setTimeout('window.location.reload()',1000);</script>");
            $data=json_decode($arList);
            if($data->tid>0){
                $res=Db::table('tb_webkey')->where(['ybyid'=>$data->tid])->update(['nowPm'=>$data->rank,'pmtime'=>$data->update_time]);
                print_r($res.'---'.date('Y-m-d H:i:s',time()).'---tid:'.$data->tid.'---nowPm:'.$data->rank.'---pmtime:'.$data->update_time);
            }else{
                print_r('<h1 style="text-align: center;font-size: 80px;line-height: 2">'.date('Y-m-d H:i:s',time()).'</h1>');
                print_r('<h1 style="text-align: center;font-size: 80px;line-height: 2">err<br>不能关！！！</h1>');
            }
        }else{
            echo ("<script>setTimeout('window.location.reload()',3600000);</script>");
            print_r('<h1 style="text-align: center;font-size: 80px;line-height: 2">'.date('Y-m-d H:i:s',time()).'</h1>');
            print_r('<h1 style="text-align: center;font-size: 80px;line-height: 2">err<br>不能关！！！</h1>');
        }
    }

    public function get_redis(){
        $redis = new \Redis();
        $redis->connect('127.0.0.1',6379);

//        $arList = $redis->lPop('wbKeyList'); //输出并删除

        $arList = $redis->lrange("wbKeyList", 0 ,-1);
        dump('wbKeyList<br>');
        dump($arList);
//        dump(json_decode($arList));
//        dump($arList[0]);
//        dump(json_decode($arList[0]));
    }

    public function index()
    {
//        echo phpinfo();exit();

//        echo "Stored string in redis";


    }


    public function returnTopMoney()
    {
        $rows=Db::table('tb_Money m')
            ->field('u.money umoney,u.moneypay upay,w.istop,w.topMoney,w.nowtop,k.moneypay,k.iskf,m.*')
            ->join('tb_user u','u.id=m.userid')
            ->join('tb_webinfo w','w.id= m.webid')
            ->join('tb_webkey k','k.id= m.keyid')
            ->where([
                'm.keyid'=>['gt',0],
                'w.nowtop'=>['gt',Db::raw('topMoney')]
            ])
            ->whereTime('m.intime', 'today')
            ->select();

        foreach ($rows as $k => $v) {
            $webinfo=Db::table('tb_webinfo')->field('topMoney,nowtop')->where(['id'=>$v['webid'],])->find();
            if($webinfo['nowtop']>$v['topMoney']){
                print_r('封顶：'.$v['topMoney'].'--扣除'.$webinfo['nowtop'].'需要返还<br>');
//                减去封顶 当前扣费 -
                Db::table('tb_webinfo')->where(['id' => $v['webid'], 'istop' => 1])->setDec('nowtop', $v['money']);

                //          更新该词 消费金额 - ，扣费状态还原
                Db::table('tb_webkey')->where(['id' => $v['keyid']])->update(['moneypay'=>Db::raw('moneypay-'.$v['money']), 'iskf'=>0,]);

//              更新 用户余额 + ，消费金额 -
                Db::table('tb_user')->where('id', $v['userID'])->update([
                    'moneypay'=>Db::raw('moneypay-'.$v['money']),
                    'money'=>Db::raw('money+'.$v['money'])
                ]);

                //          tb_money 表 删除 当前 数据
                Db::table('tb_money')->where('id',$v['id'])->delete();
            }
//

//////


        }
//        dump($rows);
    }



//    检查欠费网站的状态
    public function updateqf()
    {

        $rows=Db::table('tb_webkey k')
            ->field('u.name uname,u.qftime uqf,w.id webid,w.qftime wqf,k.qftime kqf,k.id keyid,k.name kname,w.istop wistop,k.istop,w.topMoney,w.nowtop,k.iskf')
            ->join('tb_user u','u.id=k.userid')
            ->join('tb_webinfo w','w.id= k.webid')
            ->where([
                'u.money'=>['lt',0],
                'u.state'=>1,
                'k.qftime'=>null,
            ])
            ->select();
        foreach ($rows as $k => $v) {
            Db::table('tb_webinfo')->where(['id'=>$v['webid'],'qftime'=>null])->update(['qftime'=>$v['uqf']]);
            Db::table('tb_webkey')->where(['id'=>$v['keyid'],'qftime'=>null])->update(['qftime'=>$v['uqf']]);
            print_r('网站id：'.$v['webid'].'更新欠费时间---关键词id：'.$v['keyid'].'更新欠费时间'.$v['uqf'].'<br>');
        }
    }




//    更新关键词 sid
    public function updatekey()
    {
        $rows=Db::table('tb_webkey')
            ->where([
                'sid' => 0,
                'state'=>1 //正常词
            ])->select();

        foreach ($rows as $k => $v) {
            if($rows[$k]['typeid']==1){
//                $rows[$k]['sid'] =1010;
                Db::table('tb_webkey')->where(['typeid' => 1, 'id'=>$rows[$k]['id']])->update(['sid'=>1010]);
            } elseif ($rows[$k]['typeid']==2){
//                $rows[$k]['sid'] =1015;
                Db::table('tb_webkey')->where(['typeid' => 2, 'id'=>$rows[$k]['id']])->update(['sid'=>1015]);

            } elseif ($rows[$k]['typeid']==3){
//                $rows[$k]['sid'] =7010;
                Db::table('tb_webkey')->where(['typeid' => 3, 'id'=>$rows[$k]['id']])->update(['sid'=>7010]);

            }elseif ($rows[$k]['typeid']==4){
//                $rows[$k]['sid'] =1030;
                Db::table('tb_webkey')->where(['typeid' => 4, 'id'=>$rows[$k]['id']])->update(['sid'=>1030]);
            }elseif ($rows[$k]['typeid']==5){
//                $rows[$k]['sid'] =7070;
                Db::table('tb_webkey')->where(['typeid' => 5, 'id'=>$rows[$k]['id']])->update(['sid'=>7070]);
            }

        }
        return json($rows);

//        1:百度0
//        2:360
//        3:百度移动端
//        4:sogou
//        5:神马

//      1010://百度
//		1015://360
//		7010://百度移动
//		1030://搜狗
//		7070://神马
//		7015://360移动
//      7030://搜狗移动
//		7080: //头条

    }


//    更新关键词 sid
    public function getinfo()
    {

        $data = $this->request->get();

        if($data['tids']){
            $rows=jqcpm_info($data['tids']);
            return $rows;
        }else{
            return '11';
        }

    }


//关键词
    public function check_user()
    {
        $data = $this->request->post();

        if($data){
            $res=Db::table('tb_admin')
                ->where([
                    'name' => $data['username'],
                    'pwd' => substr(md5($data['password']), 8, 16)
                ])->find();

            if ($res) {
                Session::set('admin.id',$res['id']);
                Session::set('admin.name',$res['name']);
                Session::set('admin.levelNum',$res['levelNum']);
                Session::set('admin.departid',$res['departID']);

                if(Session::get('admin')){
                    return json(['msg' =>'登录成功', 'code' =>0]);
                }else{
                    return json(['msg' =>'登录失败', 'code' =>1]);
                }

            }else{
                return json(['msg' =>'用户名或者密码错误!', 'code' =>1]);
            }
        }
    }


}
